<?php
/**
 * Created by PhpStorm.
 * User: CPR065
 * Date: 2016/8/9
 * Time: 10:56
 */

namespace Admin\Event;


class ScanEvent
{
    /**扫描权限
     * @param $moduleDir
     * @return mixed
     */
    public function scan($moduleDir)
    {

        $contrPath = $moduleDir . 'Controller';
        $rst = $this->getAllContro($contrPath);
        if ($rst['status'] == 1) {
            $info = '';
            $contrArr = $rst['data']; //控制器信息
            foreach ($contrArr as $k => $v) {
                $methodInfoArr = $this->getAllMethod($v['contro'], $contrPath . '/' . $v['file']);
                $info.=$this->saveRule($methodInfoArr);
            }
        } else {
            return $rst['info'];
        }
    }

    /**
     * 保存扫描的信息
     * @param $list
     */
    public function saveRule($list)
    {
        $Model = M('admin_rule');
        $info = '';
        foreach($list as $k =>$v){
            $data['title'] = $v['description'];
            $data['module'] = $v['module'];
            $data['url'] = $v['controller'].'/'.$v['method'];
            $data['allow_all'] = $v['allow_all'];
            if( $Model ->where("uuid = '{$v['uuid']}'")->find()){
                $rst = $Model->where("uuid = '{$v['uuid']}'")->save($data);
                if($rst !== false){
                    $info.='<p class="text-success">'.$data['title'].':'.$data['module'].':'.$data['url'].':'.$v['uuid'].'修改成功</p>';
                }else{
                    $info.='<p class="text-error">'.$data['title'].':'.$data['module'].':'.$data['url'].':'.$v['uuid'].'修改失败</p>';
                }

            }else{
                $data['uuid'] = $v['uuid'];
                $rst = $Model ->add($data);
                if($rst !== false){
                    $info.='<p class="text-success">'.$data['title'].':'.$data['module'].':'.$data['url'].':'.$v['uuid'].'添加成功</p>';
                }else{
                    $info.='<p class="text-error">'.$data['title'].':'.$data['module'].':'.$data['url'].':'.$v['uuid'].'添加失败</p>';
                }
            }
        }
       return  $info;
    }
    /***获取控制器中的方法和描述
     * @param $contro
     * @param $file
     * @return array
     */
    public function getAllMethod($contro, $file)
    {
        // print_g($file);
        $hander = fopen($file, 'r');
        $i = 0;
        $data = [];
        while ($content = fgets($hander, 1024)) {
            if (stripos($content, '@privilege') !== false) {
                $info = explode('@privilege', $content);
                $infoArr = explode(':', $info['1']);
                if ($infoArr['0']) {
                    $data[$i]['module'] = trim($infoArr['0']);
                }
                $data[$i]['controller'] = $contro;
                if ($infoArr['1']) {
                    $data[$i]['description'] = trim($infoArr['1']);
                }
                if ($infoArr['2']) {
                    $data[$i]['uuid'] = trim($infoArr['2']);
                }
                if ($infoArr['3']) {
                    $data[$i]['allow_all'] = 1;
                }else{
                    $data[$i]['allow_all'] = 0;
                }
            }
            if (stripos($content, 'public') !== false && stripos($content, 'function') !== false) {
                $info = explode('function', $content);
                $data[$i]['method'] = preg_replace('/\s|\(.*\)(\{)?/i', '', $info['1']);
                $i++;
            }
        }
        return $data;

    }

    /**获取控制器
     * @param $contrPath
     * @return array
     */
    public function getAllContro($contrPath)
    {
        if (is_dir($contrPath)) {
            $dh = opendir($contrPath);
            $i = 0;
            while (($file = readdir($dh)) !== false) {
                $notUseArr = ['.', '..'];
                if (!in_array($file, $notUseArr)) {
                    $arr = explode('Controller', $file);
                    if ($arr) {
                        $contro = $arr[0];
                        $ignoreArr = ['Test', 'Index', 'AdminBase', 'Public'];
                        if (!in_array($contro, $ignoreArr)) {
                            $fileList[$i]['contro'] = $arr[0];
                            $fileList[$i]['file'] = $file;
                            $i++;
                        }

                    } else {
                        continue;
                    }

                }
            }
            return ['status' => 1, 'data' => $fileList];
        } else {
            return ['status' => 0, 'info' => '控制器目录不存在'];
        }
    }
} 